<template>
  <!-- 修改资料 -->
  <div>
    <!-- 内容主题区域 -->
    <el-form
      label-width="110px"
      :model="form"
      label-position="right"
      ref="formRef"
      :rules="formRules"
    >
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="模块服务Id" prop="moduleId">
            <el-input v-model="form.moduleId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="模块目录" prop="catalogId">
            <el-tree-select
              v-model="form.catalogId"
              :data="parentData"
              :render-after-expand="false"
              :check-strictly="true"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="JSON配置文件" prop="moduleConfigFile">
            <el-input v-model="form.moduleConfigFile"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="服务类" prop="serivceClassName">
            <el-input v-model="form.serivceClassName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="备注">
            <el-input v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="配置文件目录">
            <el-popover
              placement="bottom-start"
              :width="540"
              v-model:visible="showChoose"
              trigger="click"
              @show="showChoose = true"
            >
              <template #reference>
                <el-input
                  v-model="form.configPath"
                  placeholder="点击选择"
                  readonly
                >
                </el-input>
              </template>
              <el-tree
                style="width: 100%"
                :data="configPathData"
                node-key="id"
                :expand-on-click-node="false"
                :check-strictly="true"
                @node-click="nodeClick"
              >
                <template #default="{ node, data }">
                  <span class="custom-tree-node">
                    <span>
                      <el-icon style="margin-right: 2px; margin-top: 2px"
                        ><Folder
                      /></el-icon>
                      <span>{{ node.label }}</span>
                    </span>

                    <span style="color: #000">
                      <el-input
                        size="small"
                        v-if="data.visible"
                        style="width: 100px; margin-right: 10px"
                        placeholder="请输入目录名称"
                        v-model="configPathText"
                      ></el-input>
                      <a @click="append(data)">{{
                        data.visible ? "确认" : "新增"
                      }}</a>
                      <a style="margin-left: 8px" @click="remove(data)">
                        删除
                      </a>
                    </span>
                  </span>
                </template>
              </el-tree>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="form.status"
              size="large"
              inline-prompt
              :active-icon="Check"
              :inactive-icon="Close"
              active-value="normal"
              inactive-value="disable"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="上传附件">
        <tsUpload @updateFile="updateFile" @removeFile="removeFile"></tsUpload>
      </el-form-item>

      <sc-title title="关联表"></sc-title>
      <el-scrollbar height="300px">
        <sc-form-table
          v-model="dataList"
          :addTemplate="addTemplate"
          drag-sort
          placeholder="暂无数据"
        >
          <template #tableContent>
            <el-table-column label="数据库表名">
              <template #default="scope">
                <ts-select
                  multipe
                  v-model="scope.row.number"
                  fileName="MetaTableReport"
                  :template="tableProps"
                  @updateRow="rowChange(scope, $event)"
                  @selectRows="selectAllRow"
                  :key="selectKey"
                ></ts-select>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="数据库表描述"></el-table-column>
            <el-table-column label="表类型">
              <template #default="scope">
                <span v-if="scope.row.operationType == ''">{{
                  scope.row.tableTypeName
                }}</span>
                <el-select v-model="scope.row.tableType">
                  <el-option
                    v-for="item in configData.tableTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
          </template>
        </sc-form-table>
      </el-scrollbar>
    </el-form>
    <div class="button-position">
      <el-button @click="updateVisible">取 消</el-button>
      <el-button type="primary" @click="add(formRef)">确 定</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { ElNotification } from "element-plus";
import { Check, Close, Folder } from "@element-plus/icons-vue";
import Api from "@/api/model/meta";
import tsSelect from "@/components/scSelect/index.vue";
import tsUpload from "@/components/tsUpload/file.vue";
const emit = defineEmits(["updatetable"]);
// 添加用户数据的对象
const form = ref({});
const configData = ref({});
const parentData = ref([]);
const dataList = ref([]);
const configPathData = ref([]);
const addTemplate = ref({});
const configPathText = ref();
const selectKey = ref(0);
const showChoose = ref(false);
const formRef = ref();
const formRules = reactive({
  moduleId: [{ required: true, message: "请输入Id", trigger: "blur" }],
  number: [{ required: true, message: "请输入编号", trigger: "blur" }],
});
const tableProps = {
  value: "number",
  label: "number",
};
onMounted(() => {
  getInit();
  getTreeData();
});

const getInit = async () => {
  let params = {
    fileName: "MetaCatalogTreeReport",
  };
  let res = await Api.module.parentTree.post(params);
  if (res.code == 200) {
    parentData.value = res.reportData;
  }
  let formRes = await Api.module.add.post();
  if (formRes.code == 200) {
    form.value = formRes.data;
    configData.value = formRes.configData;
    addTemplate.value = formRes.dtl;
  }
};
const getTreeData = async () => {
  let res = await Api.module.searchConfigPath.post();
  console.log(res);
  if (res.code == 200) {
    configPathData.value = res.dataList;
  }
};
const updateFile = (val) => {
  form.value.file = val;
};
const removeFile = () => {
  form.value.file = undefined;
};
const append = async (data) => {
  if (!data.visible) {
    data.visible = true;
  } else {
    if (!configPathText.value) {
      ElNotification.warning({
        title: "",
        message: "请输入目录名称",
        duration: 2000,
      });
    } else {
      let path = data.value + "/" + configPathText.value;
      let res = await Api.module.createModuleDirectory.post({ path: path });
      if (res.code == 200) {
        data.visible = false;
        configPathText.value = "";
        getTreeData();
      }
    }
  }
};
const remove = async (data) => {
  let res = await Api.module.deleteModuleDirectory.post({ path: data.value });
  if (res.code == 200) {
    getTreeData();
  }
};
const nodeClick = (data) => {
  form.value.configPath = data.value;
  showChoose.value = false;
};
const rowChange = (scope, val) => {
  dataList.value[scope.$index] = {name:val.name,number:val.number};
};
const selectAllRow = (val) => {
  val.forEach((ele) => {
    dataList.value.unshift({name:ele.name,number:ele.number});
  });
  dataList.value.forEach((ele, num) => {
    if (!ele.number && num + 1 !== dataList.value.length) {
      dataList.value.splice(num, 1);
    }
  });
  selectKey.value += 1;
};
const restAddFormData = () => {
  formRef.value.resetFields();
};

const updateVisible = (val) => {
  emit("updatetable");
};

// 上传
const add = async (formRef) => {
  await formRef.validate((valid, fields) => {
    if (valid) {
      addData();
    }
  });
};

const addData = async () => {
  console.log(dataList.value)
  let params = {
    bean: form.value,
    dataList: dataList.value.filter(ele=>{return ele.number})
  };
  console.log(params)
  let res = await Api.module.save.post(params);
  if (res.code == 200) {
    restAddFormData();
    ElNotification.success({
      title: "",
      message: "添加成功！",
      duration: 2000,
    });
    emit("updatetable", true);
  }
};
</script>

<style lang="less" scoped>
.el-dialog__wrapper {
  pointer-events: none;
}

.el-dialog {
  pointer-events: auto;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
a {
  font-weight: 500;
  text-decoration: inherit;
  color: #409eff;
}
</style>

